package com.lizanyi.teaFriend.controller;

import com.github.pagehelper.PageInfo;
import com.lizanyi.teaFriend.pojo.TeaAdmin;
import com.lizanyi.teaFriend.service.AdminService;
import com.lizanyi.teaFriend.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class AmdinManage {
    @Autowired
    private AdminService adminService;
    @Autowired
    private PageService pageService;

    @RequestMapping("/to/adminmanage")
    public String toAdminManage(){
        return "redirect:/to/adminmanage/selectall";
    }

    //根据account查询管理员
    @RequestMapping("/to/adminmanage/select")
    public String select(@RequestParam("account") String account,
                         @RequestParam(value = "pageNum" ,required = false,defaultValue = "1")  Integer pageNum,
                         @RequestParam(value = "pageSize",required = false) Integer pageSize,
                         ModelMap modelMap){
        PageInfo<TeaAdmin> pageInfo=pageService.findTeaAdmin(pageNum,pageSize);
        modelMap.addAttribute("pageInfo",pageInfo);
        if(account.isEmpty()){
            modelMap.addAttribute("toerror","账号不能为空");
            List<TeaAdmin> teaAdmins =pageInfo.getList();
            modelMap.addAttribute("teaAdmins",teaAdmins);
        }else {
            TeaAdmin teaAdmin=adminService.selectByAccount(account);
            modelMap.addAttribute("teaAdmins",teaAdmin);
        }

        return "adminManage";
    }

    //查询全部管理员
    @RequestMapping(value = "/to/adminmanage/selectall",method = RequestMethod.GET)
    public String selectall(@RequestParam(value = "pageNum" ,required = false,defaultValue = "1")  Integer pageNum,
                            @RequestParam(value = "pageSize",required = false) Integer pageSize,
                            ModelMap modelMap){
        PageInfo<TeaAdmin> pageInfo=pageService.findTeaAdmin(pageNum,pageSize);
        modelMap.addAttribute("pageInfo",pageInfo);
        List<TeaAdmin> teaAdmins =pageInfo.getList();
        modelMap.addAttribute("teaAdmins",teaAdmins);
        return "adminManage";
    }
    //分页查询管理员
    @RequestMapping(value = "/to/adminmanage/selectall/{pageNum}",method = RequestMethod.GET)
    public String selectall1(@PathVariable(value = "pageNum" ,required = false)  Integer pageNum,
                            @PathVariable(value = "pageSize",required = false) Integer pageSize,
                            ModelMap modelMap){
        PageInfo<TeaAdmin> pageInfo=pageService.findTeaAdmin(pageNum,pageSize);
        modelMap.addAttribute("pageInfo",pageInfo);
        List<TeaAdmin> teaAdmins =pageInfo.getList();
        modelMap.addAttribute("teaAdmins",teaAdmins);
        return "adminManage";
    }

    //更新管理员信息
    @RequestMapping("/to/adminmanage/update")
    public String updateAdmin(@RequestParam("account") String account,
                              @RequestParam("password") String password,
                              ModelMap modelMap){
        int flag=adminService.updateTeaAdmin(account,password);
        if(flag>0){
            modelMap.addAttribute("message","更新成功");
        }else {
            modelMap.addAttribute("message","更新失败");
        }

        return "redirect:/to/adminmanage/selectall";
    }

    @RequestMapping("/to/adminmanage/insert")
    public String adminInsert(@RequestParam("account") String account,
                              @RequestParam("password") String password,
                              ModelMap modelMap) {
       int flag= adminService.addTeaAdmin(account,password);
        if(flag>0){
            modelMap.addAttribute("message","添加成功");
        }else {
            modelMap.addAttribute("message","添加失败");
        }
        return "redirect:/to/adminmanage/selectall";
    }

    @RequestMapping(value = "/to/adminmanage/delete/{account}",method = RequestMethod.DELETE)
    public String deleteAdmin(@PathVariable("account") String account,
                              ModelMap modelMap) {
        int flag= adminService.deleteAdmin(account);
        if(flag>0){
            modelMap.addAttribute("message","删除成功");
        }else {
            modelMap.addAttribute("message","删除失败");
        }
        return "redirect:/to/adminmanage/selectall";
    }



}
